<template>
  <div class="StatisticsAll">
    <el-form
      ref="form"
      :model="form"
      label-position="top"
      class="myform"
      size="mini"
    >
      <div>
        <el-form-item label="单号" class="myform-item">
          <el-input
            v-model="form.number"
            placeholder="请输入单号"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="类型" class="myform-item">
          <el-select v-model="form.groups" placeholder="请选择" clearable>
            <el-option
              v-for="item in optionsLX"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="审批状态" class="myform-item">
          <el-select v-model="form.status" placeholder="请选择" clearable>
            <el-option
              v-for="item in optionsSP"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="归还状态" class="myform-item">
          <el-select v-model="form.is_return" placeholder="请选择" clearable>
            <el-option
              v-for="item in optionsGH"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="发起时间" class="myform-item">
          <el-date-picker
            clearable
            v-model="form.strtime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item style="margin-top: 38px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
      <div></div>
    </el-form>
    <el-table :data="tableData" border style="width: 100%" size="mini">
      <el-table-column prop="" label="" width="55">
        <template slot-scope="{ row }">
          <el-checkbox
            v-model="row.is"
            :disabled="
              !(row.is_push == 0 && row.status == 1 && row.data_status == 0)
            "
          ></el-checkbox>
        </template>
      </el-table-column>
      <el-table-column prop="allocation_number" label="单号"> </el-table-column>
      <!-- <el-table-column prop="instance_code" label="审批id"> </el-table-column> -->
      <el-table-column prop="adduser" label="发起人"> </el-table-column>
      <el-table-column prop="createtime" label="发起时间"> </el-table-column>
      <el-table-column prop="groups" label="类型">
        <template slot-scope="{ row }">
          {{ row.groups | groupsTitle }}
        </template>
      </el-table-column>
      <el-table-column prop="data_status" label="单号状态">
        <template slot-scope="{ row }">
          <div
            :style="{
              color:
                row.data_status == 0
                  ? '#67C23A'
                  : row.data_status == 1
                  ? '#F56C6C'
                  : '',
            }"
          >
            {{
              row.data_status == 0 ? "正常" : row.data_status == 1 ? "关闭" : ""
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="type_no" label="款号">
        <template slot-scope="{ row }">
          {{ row.type_no | arrString }}
        </template>
      </el-table-column>
      <el-table-column prop="jh_jc_num" label="计划/实际数量">
        <template slot-scope="{ row }">
          {{ row.jh_jc_num || 0 }}/
          <span style="color: red">{{ row.sj_jc_num || 0 }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="sj_gh_num" label="归还数量">
        <template slot-scope="{ row }">
          {{ row.sj_gh_num || 0 }}
        </template>
      </el-table-column> -->
      <el-table-column prop="date" label="计划发货日期">
        <template slot-scope="{ row }">
          {{row.date || '立即发货'}}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="审核状态">
        <template slot-scope="{ row }">
          <div
            :style="{
              color:
                row.status == 0
                  ? '#E6A23C'
                  : row.status == 1
                  ? '#67C23A'
                  : row.status == 2
                  ? '#F56C6C'
                  : row.status == 3
                  ? '#909399'
                  : '',
            }"
          >
            {{
              row.status == 0
                ? "审核中"
                : row.status == 1
                ? "已通过"
                : row.status == 2
                ? "已拒绝"
                : row.status == 3
                ? "已撤销"
                : ""
            }}
            <!-- <span
              @click="onLookRun(row.allocation_number, 8, row.run_id)"
              style="color: #2c70d6"
              class="pointer"
              >（查看审批）</span
            > -->
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="data_return" label="归还状态">
        <template slot-scope="{ row }">
          <div
            :style="{
              color:
                row.data_return == 1
                  ? '#E6A23C'
                  : row.data_return == 2
                  ? '#67C23A'
                  : '',
            }"
          >
            {{
              row.data_return == 0
                ? "未归还"
                : row.data_return == 1
                ? "部分归还"
                : row.data_return == 2
                ? "全部归还"
                : ""
            }}
          </div>
        </template>
      </el-table-column> -->
      <el-table-column prop="is_push" label="推送状态">
        <template slot-scope="{ row }">
          <div
            :style="{
              color:
                row.is_push == 1
                  ? '#67C23A'
                  : row.is_push == 2
                  ? '#F56C6C'
                  : row.is_push == 3
                  ? '#909399'
                  : '',
            }"
          >
            {{
              row.is_push == 0
                ? "未推送"
                : row.is_push == 1
                ? "成功"
                : row.is_push == 2
                ? "失败"
                : row.is_push == 3
                ? "取消"
                : ""
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template scope="{ row }">
          <el-button
            v-if="row.is_push == 0 && row.status == 1 && row.data_status == 0"
            type="text"
            size="default"
            @click="onPush(row)"
            >推送巨沃</el-button
          >
          <el-button type="text" size="default" @click="onLook(row)"
            >查看</el-button
          >
          <!-- <el-button
            v-if="row.status == 1 && row.is_push == 1"
            style="color: red"
            type="text"
            size="default"
            @click="onCancel(row)"
            >取消</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px">
      <el-button type="primary" size="mini" @click="onPushAll"
        >批量推送巨沃</el-button
      >
    </div>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminOmsGetAllocationtjAllllist_kefu"
    />
  </div>
</template>

<script>
import pagination from "@/components/pagination.vue";
import {
  postAdminOmsGetAllocationtjAllllist_kefu,
  postAdminAdminOmsCancelAllocation,
  postAdminOmspushjw_allocation,
} from "@/api";
export default {
  components: { pagination },
  filters: {
    arrString(value) {
      let string = "";
      for (let index = 0; index < value.length; index++) {
        if (index != 0) {
          string = string + "," + value[index].type_no;
        } else {
          string = value[index].type_no;
        }
      }
      return string;
    },
    groupsTitle(e) {
      if (e == 1) return "赠送";
      if (e == 2) return "借出";
      if (e == 3) return "归还";
      return e;
    },
    statusTitle(e) {
      if (e == 0) return "审核中";
      if (e == 1) return "已通过";
      if (e == 2) return "已拒绝";
      if (e == 3) return "已撤销";
      return e;
    },
    is_returnTitle(e) {
      if (e == 0) return "未归还";
      if (e == 1) return "部分归还";
      if (e == 2) return "全部归还";
      return e;
    },
  },
  data() {
    return {
      optionsSP: [
        {
          value: "0",
          label: "审核中",
        },
        {
          value: "1",
          label: "已通过",
        },
        {
          value: "2",
          label: "已拒绝",
        },
        {
          value: "3",
          label: "已撤销",
        },
      ],
      optionsGH: [
        {
          value: "0",
          label: "未归还",
        },
        {
          value: "1",
          label: "部分归还",
        },
        {
          value: "2",
          label: "全部归还",
        },
      ],
      optionsLX: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "1",
          label: "赠送",
        },
        {
          value: "2",
          label: "借出",
        },
      ],
      form: {
        is_push:0,
        number: "",
        groups: "",
        status: 1,
        is_return: "",
        strtime: "",
        endtime: "",
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 0,
    };
  },
  activated() {
    this.postAdminOmsGetAllocationtjAllllist_kefu(this.form);
  },
  methods: {
    async onPushAll() {
      let that = this;
      let { tableData } = this;
      let allocation_number = [];

      for (let index = 0; index < tableData.length; index++) {
        if (tableData[index].is) {
          allocation_number.push(tableData[index].allocation_number);
        }
      }

      if (allocation_number.length) {
        this.$confirm("确定推送?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            let obj = {
              allocation_number,
            };
            try {
              await postAdminOmspushjw_allocation(obj);
              this.$message.success("推送成功");
              await that.postAdminOmsGetAllocationtjAllllist_kefu(that.form);
            } catch (error) {}
          })
          .catch(() => {});
      }
    },
    async onPush(row) {
      let obj = {
        allocation_number: [row.allocation_number],
      };
      this.$confirm("确定推送?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            await postAdminOmspushjw_allocation(obj);
            this.$message.success("推送成功");
            await that.postAdminOmsGetAllocationtjAllllist_kefu(that.form);
          } catch (error) {}
        })
        .catch(() => {});
    },
    onCancel(e) {
      let _this = this;
      let data = {
        allocation_number: e.allocation_number,
        order_type: "TRAN",
      };
      this.$confirm("确定取消调拨?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await postAdminAdminOmsCancelAllocation(data);
          this.postAdminOmsGetAllocationtjAllllist_kefu(_this.form);
          this.$message({
            type: "success",
            message: "成功!",
          });
        })
        .catch(() => {});
    },
    onLook(e) {
      if (e.groups == 1) {
        this.$router.push({
          name: "StatisticsAdminZ",
          query: { allocation_number: e.allocation_number },
        });
      } else {
        this.$router.push({
          name: "StatisticsAllAdmin",
          query: { allocation_number: e.allocation_number },
        });
      }
    },
    // 审批进度
    onLookRun(id, type, run_id) {
      this.$router.push({ name: "contractrunid", query: { id, type, run_id } });
    },
    async postAdminOmsGetAllocationtjAllllist_kefu(data) {
      console.log("data", data);
      this.form = data;
      let form = JSON.parse(JSON.stringify(data));
      console.log("data", data);
      if (form.strtime) {
        form.strtime = `${data.strtime[0]}`;
        form.endtime = `${data.strtime[1]}`;
      } else {
        form.strtime = "";
        form.endtime = "";
      }
      let res = await postAdminOmsGetAllocationtjAllllist_kefu(form);
      let tableData = res.data;
      tableData = tableData.map((e) => {
        e.is = false;
        return e;
      });
      this.tableData = tableData;
      this.total = Number(res.total);
    },
    onSearch() {
      let { form } = this;
      form.page = 1;
      form.size = 10;
      this.postAdminOmsGetAllocationtjAllllist_kefu(form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.StatisticsAll {
  background: #fff;
  padding: 10px;
}
</style>